import { Toast } from "vant";
import cTitle from "components/title";
import { scrollMixin } from "utils/mixin";
import BScroll from "better-scroll";
import indexGoods from "../sachertorteComm/indexGoodsItem";
export default {
  components: {
    cTitle,
    indexGoods
  },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      swiperOption: {
        direction: "horizontal",
        autoHeight: true,
        autoplay: false,
        pagination: false,
        loop: true,
        initialSlide: 0
      },
      banner: [], //轮播图
      notice: "", //公告
      category: [], //分类
      goods: [], //抢购列表
      brand: [], //品牌
      advertising: {},
      recommandList: [],
      purchaseTime: "",
      timeData: {},
      Hei: "", //这是swiper要动态设置的高度属性
      page: 1,
      totalPage: 0,
      isLoadMore: true,
      city: "",
      point: {},
      background_color:'#ffc11e'
    };
  },
  activated() {
    this.page = 1;
    if (this.$route.query.city) {
      this.city = this.$route.query.city.trim();
    }
    this.fun
      .getLocation()
      .then(res => {
        this.point = res.point;
        console.log(res.point)
        this.locationStatus = true;
        if (this.city === "") {
          this.city = res.city;
        }
        this.frontendIndex();
        this.getBand();
      })
      .catch(err => {
        console.log(err);
        this.frontendIndex();
        this.getBand();
      });
    //需要有默认数据显示
    this.frontendIndex();
  },
  methods: {
    onTimeChange(e) {
      this.timeData = e.detail;
    },
    setDistance(distance) {
      if (distance >= 1000) {
        return Number(distance / 1000).toFixed(2) + 'Km'
      } else {
        return distance + 'm'
      }
    },
    swiperNav(url) {
      console.log(url);
      if (url) {
        window.location.href = url;
      }
    },
    _initScrollCart() {
      if (this.brand.length > 0) {
        this.storeScroll = new BScroll(this.$refs.handpickList, {
          scrollX: true,
          click: true
        });
      }
    },
    _initScrollTime() {
      if (this.goods.length > 0) {
        this.storeScroll = new BScroll(this.$refs.timeList, {
          scrollX: true,
          click: true
        });
      }
    },
    addressTo() {
      this.$router.push(this.fun.getUrl("citypickerPage", {}, { plu: "cake" }));
    },
    showStore() {
      this.$nextTick(() => {
        if (!this.storeScroll) {
          console.log("执行成功");
          // this._initScrollCart();
          this._initScrollTime();
        } else {
          this.storeScroll.refresh();
        }
      });
    },
    handpickNav(id = "") {
      this.$router.push(this.fun.getUrl("sachertorteClassify", {}, { brandId: id, city: this.city }));
    },
    toBand(item){
      this.$router.push(this.fun.getUrl("sachertorteBandShop", {brand_id:item.id}, {city:this.city}));
    },
    toAdv(url){
      if(!url){
        this.$router.push(this.fun.getUrl("sachertorteClassify", {}, { id: item.id, classifyName: item.name, city: this.city }));
        return
      }
      window.location.href = url;
    },
    classifyNav(id = "", name) {
      console.log(name)
      if(name == '品牌馆'){
        this.$router.push(this.fun.getUrl("sachertorteBand", {}, {city: this.city}));
        return
      }
      this.$router.push(this.fun.getUrl("sachertorteClassify", {}, { id: id, classifyName: name, city: this.city }));
    },
    searchNav() {
      this.$router.push(this.fun.getUrl("sachertorteSearch", {}, { city: this.city }));
    },
    purchaseOn() {
      this.$router.push(this.fun.getUrl("sachertortePurchase", {}, { city: this.city }));
    },
    goodsNav(id) {
      this.$router.push(this.fun.getUrl("sachertorteGoods", {}, { id: id, city: this.city }));
    },
    adverNav(url) {
      if (url) {
        window.location.href = url;
      }
    },
    frontendIndex() {
      let query = {
        city: this.city
      };
      $http.get("plugin.yz-supply-cake.frontend.index.index", query, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            let goods = res.goods;
            let date = new Date(goods[0]?.promotion_end_at?.replace(/-/g,'/'));
            let timeDate = date.getTime();
            let current = new Date().getTime();
            this.advertising = res.advertising;
            this.goods = res.goods;
            this.category = res.category;
            let advc = [{ name: '品牌馆', thumb_url: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2Fsachertorte%2Fband%402x.png' }]
            this.category = [...this.category, ...advc]
            this.banner = res.banner;
            this.brand = res.brand;
            this.notice = res.notice;
            this.purchaseTime = timeDate - current;
            this.showStore();
            let info_basic = JSON.parse(localStorage.getItem("yz_basic_info"));
            this.background_color = info_basic.theme_color;
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function (response) {
          // console.log(response);
          // error callback
        }
      );
    },
    // goodsByRecommand() {
    //   let query = {
    //     page: this.page,
    //     city: this.city
    //   };
    //   $http.get("plugin.yz-supply-cake.frontend.goods.goods.getGoodsByRecommand", query, "正在获取").then(
    //     response => {
    //       if (response.result == 1) {
    //         let res = response.data;
    //         this.isLoadMore = true;
    //         this.total_page = res.last_page;
    //         if (!this.total_page) {
    //           this.query.page = 0;
    //         }
    //         this.recommandList = res.data;
    //       } else {
    //         this.page = this.page - 1;
    //         this.isLoadMore = false;
    //         this.$dialog.alert({ message: response.msg });
    //       }
    //     },
    //     function(response) {
    //       // console.log(response);
    //       // error callback
    //     }
    //   );
    // },
    getBand() {
      let json = {
        city: this.city,
        page: this.page,
        lng: this.point.lng ? this.point.lng : '',
        lat: this.point.lat ? this.point.lat : '',
        pageSize: 5,
        is_recommend :1
      }
      $http.get("plugin.yz-supply-cake.frontend.goods.goods.getBrandAndGoodsAndStore", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.isLoadMore = true;
            this.total_page = res.last_page;
            if (!this.total_page) {
              this.page = 0;
            }
            this.recommandList = res.data;
          } else {
            this.page = this.page - 1;
            this.isLoadMore = false;
            this.$dialog.alert({ message: response.msg });
          }
        },
        function (response) {
          // console.log(response);
          // error callback
        }
      );
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let json = {
          city: this.city,
          page: this.page,
          lng: this.point.lng ? this.point.lng : '',
          lat: this.point.lat ? this.point.lat : '',
          pageSize: 10,
          is_recommend :1
        }
        $http.get("plugin.yz-supply-cake.frontend.goods.goods.getBrandAndGoodsAndStore", json, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let myData = res.data.data;
              this.recommandList = [...this.recommandList, ...myData]; //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function (response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    }
  }
};
